﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Configuration;
using System.Configuration;
using System.Web.Configuration;
using System.Web;
using Marbles.Core.Encryption;

namespace Marbles.Core.Settings
{
    public class MailSettings
    {

        public string Host { get { return _host; } set { _host = value; } }
        public Int32 Port { get { return _port; } set { _port = value; } }
        public string Username { get { return _username; } set { _username = value; } }
        public string Password { get { return _password; } set { _password = value; } }

        private string _host = string.Empty;
        private Int32 _port = 25;
        private string _username = string.Empty;
        private string _password = string.Empty;

        public MailSettings(MailSettingsSectionGroup Settings)
        {
            _host = EncryptionProvider.Decode1(Settings.Smtp.Network.Host, EncryptionProviderType.TripleDES);
            _port = Settings.Smtp.Network.Port;
            _username = EncryptionProvider.Decode1(Settings.Smtp.Network.UserName, EncryptionProviderType.TripleDES);
            _password = EncryptionProvider.Decode1(Settings.Smtp.Network.Password, EncryptionProviderType.TripleDES);
        }

        public MailSettings()
        {            
            Configuration config = WebConfigurationManager.OpenWebConfiguration("~/");
            MailSettingsSectionGroup Settings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
            _host = EncryptionProvider.Decode1(Settings.Smtp.Network.Host, EncryptionProviderType.TripleDES);
            _port = Settings.Smtp.Network.Port;
            _username = EncryptionProvider.Decode1(Settings.Smtp.Network.UserName, EncryptionProviderType.TripleDES);
            _password = EncryptionProvider.Decode1(Settings.Smtp.Network.Password, EncryptionProviderType.TripleDES);
        }

        public static System.Net.Mail.SmtpClient CreateSmtpClient()
        {
            MailSettings config = new MailSettings();
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(config.Host);
            if (!string.IsNullOrEmpty(config.Username))
            {
                client.Credentials = new System.Net.NetworkCredential(config.Username, config.Password);
            }
            return client;
        }

        internal void UpdateMailSettings(MailSettings value)
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration("~/");
            MailSettingsSectionGroup mailsettings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");

            if (value == null)
            {
                mailsettings.Smtp.Network.Host = EncryptionProvider.Encode1("", EncryptionProviderType.TripleDES);
                mailsettings.Smtp.Network.Port = 25;
                mailsettings.Smtp.Network.UserName = EncryptionProvider.Encode1("", EncryptionProviderType.TripleDES);
                mailsettings.Smtp.Network.Password = EncryptionProvider.Encode1("", EncryptionProviderType.TripleDES);
            }
            else
            {
                mailsettings.Smtp.Network.Host = EncryptionProvider.Encode1(value.Host, EncryptionProviderType.TripleDES);
                mailsettings.Smtp.Network.Port = _port;
                mailsettings.Smtp.Network.UserName = EncryptionProvider.Encode1(value.Username, EncryptionProviderType.TripleDES);
                mailsettings.Smtp.Network.Password = EncryptionProvider.Encode1(value.Password, EncryptionProviderType.TripleDES);
            }

                config.Save();
        }


    }
}
